package com.openarena.logic.utils;

import com.openarena.model.TeamEnum;
import java.io.Closeable;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MyUtils {

    private static final Logger logger = Logger.getLogger(MyUtils.class.getName());

    public static boolean isNull(String s) {
        return (s == null || s.length() == 0 || s.trim().length() == 0);
    }
    
    public static void silentClose(Closeable closeable) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (Exception ex) {
                logger.log(Level.SEVERE, "Could not close input", ex);
            }
        }
    }

    public static TeamEnum contrario(TeamEnum team) {
        if (team == null) {
            return null;
        }

        switch (team) {
            case BLUE:
                return TeamEnum.RED;
            case RED:
                return TeamEnum.BLUE;
        }

        return null;
    }

    public static int diferenciaEnDias(Date date1, Date date2) {

        Date dateInicial;
        Date dateFinal;
        if (date1.before(date2)) {
            dateInicial = date1;
            dateFinal = date2;
        } else {
            dateInicial = date2;
            dateFinal = date1;
        }

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dateInicial);

		int days = 0;

		while (calendar.getTime().before(dateFinal)) {
			calendar.add(Calendar.DATE,1);
			days++;
		}

		return days;
    }
}

